/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.model;

import java.util.ArrayList;

public class Operation extends ModelElement {
	private static final long serialVersionUID = -76390916188101694L;
	private String visibility;
	private String name;
	private String returnType;
	private boolean isStaticOperation = false;
	private boolean isAbstractOperation = false;
	private boolean isConstructorOperation = false;
	private boolean isDestructorOperation = false;
	private ArrayList<Parameter> parameters;

	// visibility <<constructor>>/<<destructor>> <<static>>/<<abstract>>
	// name(parameters) : returnType

	public Operation() {
		super();
		parameters = new ArrayList<Parameter>();
	}

	public Operation(String visibility, boolean isConstructor,
			boolean isDestructor, boolean isStatic, boolean isAbstract,
			String name, ArrayList<Parameter> params, String returnType) {
		int length, i;
		this.visibility = visibility;
		isConstructorOperation = isConstructor;
		isDestructorOperation = isDestructor;
		isStaticOperation = isStatic;
		isAbstractOperation = isAbstract;
		this.name = name;
		this.returnType = returnType;
		parameters = new ArrayList<Parameter>();
		length = params.size();
		for (i = 0; i < length; i++)
			parameters.add(i, params.get(i));
	}

	// firePropertyChange se apeleaza la sfarsitul secventei de editare

	public void setVisibility(String visibility) {
		this.visibility = visibility;
	}

	public void setConstructor(boolean isConstructor) {
		isConstructorOperation = isConstructor;
	}

	public void setDestructor(boolean isDestructor) {
		isDestructorOperation = isDestructor;
	}

	public void setStatic(boolean isStatic) {
		isStaticOperation = isStatic;
	}

	public void setAbstract(boolean isAbstract) {
		isAbstractOperation = isAbstract;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setParameters(ArrayList<Parameter> params) {
		int i, length;
		parameters.clear();
		length = params.size();
		for (i = 0; i < length; i++)
			parameters.add(i, params.get(i));
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
		firePropertyChange(NAME, null, returnType);
	}

	public String getVisibility() {
		return visibility;
	}

	public boolean isConstructor() {
		return isConstructorOperation;
	}

	public boolean isDestructor() {
		return isDestructorOperation;
	}

	public boolean isStatic() {
		return isStaticOperation;
	}

	public boolean isAbstract() {
		return isAbstractOperation;
	}

	public String getName() {
		return name;
	}

	public ArrayList<Parameter> getParameters() {
		return parameters;
	}

	public String getReturnType() {
		return returnType;
	}

	// visibility <<constructor>>/<<destructor>> <<static>>/<<abstract>>
	// name(parameters) : returnType

	public String getLabelText() {
		int i, length;
		String labelText = visibility + " ";
		if (isConstructorOperation)
			labelText += "<<constructor>> ";
		else if (isDestructorOperation)
			labelText += "<<destructor>> ";

		if (isStaticOperation)
			labelText += "<<static>> ";

		if (isAbstractOperation)
			labelText += "<<abstract>> ";

		labelText += name + "(";
		length = parameters.size();
		for (i = 0; i < length; i++) {
			labelText += parameters.get(i).getLabelText();
			if (i < length - 1)
				labelText += ",";
		}

		labelText += " : " + returnType;
		return labelText;
	}
}